package com.cap.dreamcircle.View;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Base.BaseActivity;
import com.cap.dreamcircle.Model.Action.RemoveFellowsForbiddensAction;
import com.cap.dreamcircle.Model.Bean.BaseBean;
import com.cap.dreamcircle.Model.Bean.FellowFansForbiddenBean;
import com.cap.dreamcircle.Model.Bean.FellowFansForbiddenEntity;
import com.cap.dreamcircle.Model.IFriendModel;
import com.cap.dreamcircle.Model.Impl.FriendModelImpl;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.CommonSubscriber;
import com.cap.dreamcircle.Utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import rx.Observable;

/**
 * Created by liucxu on 2017/10/17.
 */

public class FellowFansActivity extends BaseActivity {

    @BindView(R.id.imv_back)
    ImageView imv_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipe_refresh_layout;
    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;

    private IFriendModel model;
    private int PAGE_TYPE = 0;
    private FellowFansForbiddensAdapter adapter;
    private List<FellowFansForbiddenEntity> datas = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_fellow_fans;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        PAGE_TYPE = getIntent().getIntExtra("KEY_IS_FELLOW", 0);
        adapter = new FellowFansForbiddensAdapter(this, datas);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setItemPrefetchEnabled(false);
        recycle_view.setLayoutManager(manager);
        recycle_view.setAdapter(adapter);
        swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                requestDatas();
            }
        });
        imv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.GetInstance().finishActivity(FellowFansActivity.this);
            }
        });
    }

    @Override
    public void setupData() {
        model = new FriendModelImpl();
        requestDatas();
        EventBus.getDefault().register(this);
    }

    private void requestDatas() {
        if (PAGE_TYPE == 0) {
            tv_title.setText("我的关注");
            model.requestMyFollows().subscribe(new CommonSubscriber<FellowFansForbiddenBean>() {
                @Override
                public void onSuccess(FellowFansForbiddenBean response) {
                    dealDatasType(response.getUserArray());
                }

                @Override
                public void onFailure(BaseBean response) {
                    swipe_refresh_layout.setRefreshing(false);
                    ToastUtils.showShortToast(FellowFansActivity.this, response.getDesc());
                }
            });
        } else if (PAGE_TYPE == 1) {
            tv_title.setText("我的粉丝");
            model.requestMyFans().subscribe(new CommonSubscriber<FellowFansForbiddenBean>() {
                @Override
                public void onSuccess(FellowFansForbiddenBean response) {
                    dealDatasType(response.getUserArray());
                }

                @Override
                public void onFailure(BaseBean response) {
                    swipe_refresh_layout.setRefreshing(false);
                    ToastUtils.showShortToast(FellowFansActivity.this, response.getDesc());
                }
            });
        } else if (PAGE_TYPE == 2) {
            tv_title.setText("黑名单");
            model.requestForbiddenUsers().subscribe(new CommonSubscriber<FellowFansForbiddenBean>() {
                @Override
                public void onSuccess(FellowFansForbiddenBean response) {
                    dealDatasType(response.getUserArray());
                }

                @Override
                public void onFailure(BaseBean response) {
                    swipe_refresh_layout.setRefreshing(false);
                    ToastUtils.showShortToast(FellowFansActivity.this, response.getDesc());
                }
            });
        }
    }

    private void dealDatasType(List<FellowFansForbiddenEntity> sources) {
        swipe_refresh_layout.setRefreshing(false);
        if ((sources != null) && (sources.size() > 0)) {
            datas.clear();
            datas.addAll(sources);
            for (FellowFansForbiddenEntity single : datas) {
                single.setUserType(PAGE_TYPE);
            }
        }
        adapter.setDatas(datas);
    }

    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }

    public void onEvent(RemoveFellowsForbiddensAction action) {
        unFellowForbidden(action);
    }

    private void unFellowForbidden(final RemoveFellowsForbiddensAction action) {
        Observable<BaseBean> unForbiddenFellow = null;
        if (action.getUserType() == 2) {
            unForbiddenFellow = model.disForbiddenUser(action.getUserId());
        } else if (action.getUserType() == 0) {
            unForbiddenFellow = model.disFellow(action.getUserId());
        }
        unForbiddenFellow.subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                adapter.deleteItem(action.getPosition());
            }

            @Override
            public void onFailure(BaseBean response) {
                ToastUtils.showShortToast(FellowFansActivity.this, response.getDesc());
            }
        });
    }
}
